package com.ysy.serviceImpl;

import com.ysy.dao.NoteContentRepository;
import com.ysy.dto.NoteContentDTO;
import com.ysy.entity.NoteContent;
import com.ysy.persimmons.common.response.Response;
import com.ysy.service.NoteContentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Service
public class NoteContentServiceImpl implements NoteContentService {

    @Resource
    private NoteContentRepository noteContentRepository;

    @Override
    public Response<NoteContentDTO> selectNoteContent(NoteContentDTO noteContentDTO) {
        UUID uuid = UUID.fromString(noteContentDTO.getId());
        Optional<NoteContent> byId = noteContentRepository.findById(uuid);
        if (byId.isPresent()) {
            NoteContent noteContent = byId.get();
            NoteContentDTO noteContentBack = new NoteContentDTO();
            noteContentBack.setId(noteContentDTO.getId());
            noteContentBack.setContent(noteContent.getContent());
            return Response.success(noteContentBack);
        }
        return Response.fail("没有该笔记");
    }

    @Override
    public Response<?> addNoteContent(NoteContentDTO noteContentDTO) {
        NoteContent noteContent = new NoteContent();
        noteContent.setId(UUID.fromString(noteContentDTO.getId()));
        noteContent.setContent(noteContentDTO.getContent());
        noteContentRepository.save(noteContent);
        return Response.success();
    }

    @Override
    public Response<?> delNoteContent(NoteContentDTO noteContentDTO) {
        noteContentRepository.deleteById(UUID.fromString(noteContentDTO.getId()));
        return Response.success();
    }

    @Override
    public Response<?> updateNoteContent(NoteContentDTO noteContentDTO) {
        NoteContent noteContent = new NoteContent();
        noteContent.setId(UUID.fromString(noteContentDTO.getId()));
        noteContent.setContent(noteContentDTO.getContent());
        noteContentRepository.save(noteContent);
        return Response.success();
    }
}
